package com.mxx.pmxt.controller;

import com.mxx.pmxt.service.PUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by 马潇霄 on 2017/4/17.
 */
@Controller
public class LoginController
{

    @Resource
    PUserService pUserService;


    @RequestMapping("/checkLogin")
    @ResponseBody
    public int checkLogin(HttpServletRequest request, String uname, String upassword )
    {

        int flag = pUserService.checkLogin(uname,upassword);
        if(flag==1||flag==3){
            HttpSession session = request.getSession();
            session.setAttribute("uname", uname);
            session.setAttribute("utype", flag);
            if (flag==1){
                String account = pUserService.getUserAccount(uname);
                session.setAttribute("account",account);
            }
        }

        return pUserService.checkLogin(uname,upassword);
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session)
    {
        session.invalidate();
        return "redirect:showLogin";

    }
}
